package com.example.demo;

import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jiafei.xu@xiaobao100.com
 * @description
 * @date 2020-01-21 10:18
 */
@Slf4j
@Validated
@RestController
public class HelloController {

    @Autowired
    private ScpmerchantRest scpmerchantRest;

    @Value(value = "${scpmerchant.useHttpClient:false}")
    Boolean scpmerchantUseHttpClient;

    @Value(value = "${scpmerchant.url}")
    private String scpmerchantUrl;

    private Integer sleep = new Integer(5000);

    private Boolean accessService = Boolean.TRUE;

    @GetMapping("/hello")
    public String merchantCreditCodeInfo() {
        log.info("是否使用httpClient {}", scpmerchantUseHttpClient);
        log.info("空闲时间{}", sleep);
        try {
            Thread.sleep(sleep);
        } catch (InterruptedException e) {
            log.error("", e);
        }
        if (accessService) {
            if (!scpmerchantUseHttpClient) {
                scpmerchantRest.hello();
            } else {
                HttpUtil.createGet(scpmerchantUrl + "/hello").execute();
            }
            return "hello";
        }
        return "hello";
    }

    @GetMapping("/scpmerchantUseHttpClient/{userHttpClient}")
    public String scpmerchantUseHttpClient(@PathVariable("userHttpClient") Boolean userHttpClient) {
        scpmerchantUseHttpClient = userHttpClient;
        return "hello";
    }

    @GetMapping("/sleep/{sleepTime}")
    public String scpmerchantUseHttpClient(@PathVariable("sleepTime") Integer sleepTime) {
        sleep = sleepTime;
        return "hello";
    }

    @GetMapping("/accessService/{accessService}")
    public String accessService(@PathVariable("accessService") Boolean accessServiceValue) {
        accessService = accessServiceValue;
        return "hello";
    }

    @GetMapping("/scpmerchantUrl/{scpmerchantUrl}")
    public String scpmerchantUrl(@PathVariable("scpmerchantUrl") String scpmerchantUrlValue) {
        scpmerchantUrl = scpmerchantUrlValue;
        return "hello";
    }

}